import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path';
import copy from 'rollup-plugin-copy'

// https://vite.dev/config/
export default defineConfig({
    root:'src/',
  server: {
    port: 8080,
    host: "localhost",
    open: false,
  },
  plugins: [
    vue(),
    copy({
    targets: [
      { src: 'manifest.json', dest: 'dist' },
      { src: "src/icons/**", dest: 'dist/icons' }
    ]
  })
  ],
   resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  build: {
    outDir: path.resolve(__dirname, 'dist'),
    rollupOptions: {
      input: {
        home: path.resolve(__dirname, 'src/home/index.html'),
        popup: path.resolve(__dirname, 'src/popup/index.html'),
        devtools: path.resolve(__dirname, 'src/devtools/index.html'),
        content: path.resolve(__dirname, 'src/content/content.js'),
        background: path.resolve(__dirname, 'src/background/service-worker.js'),
      },
      output: {
        assetFileNames: 'assets/[name]-[hash].[ext]', // 静态资源
        chunkFileNames: 'js/[name]-[hash].js', // 代码分割中产生的 chunk
        entryFileNames: (chunkInfo) => { // 入口文件
          const baseName = path.basename(chunkInfo.facadeModuleId, path.extname(chunkInfo.facadeModuleId))
          const saveArr = ['content', 'service-worker']
          return `[name]/${saveArr.includes(baseName) ? baseName : chunkInfo.name}.js`;
        },
        name: '[name].js'
      }
    },
  },
})
